package web.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import exceptions.MesaException;
import exceptions.MozoException;
import beans.MesaVO;
import app.Controlador_Web;
import app.Sistema;

/**
 * Servlet implementation class ConfirmarFactura
 */
public class AbrirMesa extends HttpServlet 
{
	private static final long serialVersionUID = 1L;
       
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String nroRestS = request.getParameter("nroRest");
		String nroMesaS = request.getParameter("nroMesa");
		String cantPersonasS = request.getParameter("cantPersonas");
		int nroMesa = Integer.parseInt(nroMesaS);
		int cantPersonas = Integer.parseInt(cantPersonasS);
		int nroRest = Integer.parseInt(nroRestS);
		try 
		{
			Sistema.getInstancia().getRestauranteById(nroRest).abrirMesa(nroMesa, cantPersonas);
		} 
		catch (MozoException | MesaException e) 
		{
			e.printStackTrace();
		}
		MesaVO mesa = Controlador_Web.getInstancia().abrirMesa(nroMesaS, cantPersonasS);
		request.setAttribute("mesa", mesa);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/MesaConfirmacion.jsp");
	    dispatcher.forward(request, response);
	}
}
